<?php
declare (strict_types=1);

namespace app\advertiser\validates;

use think\Validate;

/**
 * 广告主AdvertiserMatterTaskValidate验证器
 */
class AdvertiserTaskValidate extends Validate
{
    /**
     * 验证规则
     * @var array
     */
    protected $rule = [
        'id' => 'require|number',
        'matter_id' => 'require|number',
        'task_name' => 'require|chsDash|max:50',
        'task_desc' => 'require|chsDash|max:255',
        'single_price' => 'require|float',
        'daily_limit' => 'require|float',
        'total_limit' => 'require|float',
        'contact' => 'require|chsDash|max:50',
        'contact_tel' => 'require|chsDash|checkMobile|max:50',
        'is_auto_online' => 'require|number|in:0,1',
        'sex' => 'require|number|in:0,1,2',
        'device_type' => 'require|array',
        'industry' => 'require|number|in:0',
        'location' => 'require|array',
        'region' => 'require|number|in:0',
    ];

    /**
     * 字段描述
     * @var string[]
     */
    protected $field = [
        'id' => '任务ID',
        'matter_id' => '推广物ID',
        'task_name' => '任务名称',
        'task_desc' => '任务描述',
        'single_price' => '单次价格',
        'daily_limit' => '日限额(元)',
        'total_limit' => '总限额(元)',
        'contact' => '联系人',
        'contact_tel' => '联系人电话',
        'is_auto_online' => '审核通过后自动上线',
        'sex' => '性别限制',
        'device_type' => '投放设备类型',
        'industry' => '所属行业',
        'location' => '投放地点',
        'region' => '投放范围',
    ];

    /**
     * 验证场景
     * @var array
     */
    protected $scene = [
        'add' => ['matter_id', 'task_name', 'task_desc', 'single_price', 'daily_limit', 'total_limit',
            'contact', 'contact_tel', 'is_auto_online', 'sex', 'device_type', 'industry', 'location', 'region'],
        'edit' => ['id', 'matter_id', 'task_name', 'task_desc', 'single_price', 'daily_limit', 'total_limit',
            'contact', 'contact_tel', 'is_auto_online', 'sex', 'device_type', 'industry', 'location', 'region'],
    ];

    /**
     * 验证手机号格式
     * @param string $value
     * @param string $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkMobile(string $value, string $rule, array $data)
    {
        $preg = '/^1[3-9]\d{9}$/';
        if (!preg_match($preg, $value)) {
            return '联系人手机号码格式错误';
        }
        return true;
    }

}